<%@page import="Connectdata.connectdatabase" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.text.DecimalFormat"%>


<%
//remove
            if (request.getParameter("idsp") != null && request.getParameter("idsp") != "") {
                int xoasp = Integer.parseInt(request.getParameter("idsp").toString());
                for (int i = 1; i <= Integer.parseInt(session.getAttribute("somathang").toString()); i++) {
                    if (session.getAttribute("idSP" + i) != null) {
                        int ffff = Integer.parseInt(session.getAttribute("idSP" + i).toString());
                        if (xoasp == ffff) {
                            session.removeAttribute("idSP" + i);
                            session.removeAttribute("name" + i);
                            session.removeAttribute("price" + i);
                            session.removeAttribute("soluong" + i);
                            session.setAttribute("demsorow", Integer.parseInt(session.getAttribute("demsorow").toString()) - 1);
                        }
                    }
                }
            }

//update
            if (request.getParameter("value") != null && request.getParameter("id") != null) {
                String valu = request.getParameter("value").toString();
                String idSP = request.getParameter("id").toString();
                for (int temp_update_giohang = 1; temp_update_giohang <= Integer.parseInt(session.getAttribute("somathang").toString()); temp_update_giohang++) {
                    if (session.getAttribute("idSP" + temp_update_giohang) != null && Integer.parseInt(session.getAttribute("idSP" + temp_update_giohang).toString()) != 0) {

                        if (idSP.equals(session.getAttribute("idSP" + temp_update_giohang).toString())) {


                            if (Integer.parseInt(valu) > 0 && Integer.parseInt(valu) < 10) {
                                session.setAttribute("soluong" + temp_update_giohang, valu);
                            }
                        }

                    }
                }
            }
%>

<%
            double total = 0;
            DecimalFormat df = new DecimalFormat("###,###,##0");
            if (Integer.parseInt(session.getAttribute("demsorow").toString()) > 0) {
                boolean flag_total = true;
%>

<table style="width: 680px;color: black">
    <tr style="height: 40px" >

        <th style="width: 180px" align="left">Name Product</th>
        <th style="width: 50px"  align="center">Quantity </th>
        <th style="width: 200px" >Price </th>
        <th style="width: 200px">Total </th>
        <th style="width: 50px"> </th>

    </tr>
    <%
        for (int temp_giohang = 1; temp_giohang <= Integer.parseInt(session.getAttribute("somathang").toString()); temp_giohang++) {

            if (session.getAttribute("somathang") != null && session.getAttribute("idSP" + temp_giohang) != null) {
    %>
    <tr style="height: 60px">
        <%

        %>

        <td><% out.print(session.getAttribute("name" + temp_giohang).toString());%></td>
        <td align="center">
            <select style="width: 40px" id="<% out.print("cb" + session.getAttribute("idSP" + temp_giohang).toString());%>" onchange="changevaluequantity(getvalue('<% out.print("cb" + session.getAttribute("idSP" + temp_giohang).toString());%>'),<% out.print(session.getAttribute("idSP" + temp_giohang).toString());%>)">
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("1")) {
                                                        out.print("selected='selected'");
                                                    }%> value="1" >1</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("2")) {
                                                        out.print("selected='selected'");
                                                    }%> value="2" >2</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("3")) {
                                                        out.print("selected='selected'");
                                                    }%> value="3" >3</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("4")) {
                                                        out.print("selected='selected'");
                                                    }%> value="4" >4</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("5")) {
                                                        out.print("selected='selected'");
                                                    }%> value="5" >5</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("6")) {
                                                        out.print("selected='selected'");
                                                    }%> value="6" >6</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("7")) {
                                                        out.print("selected='selected'");
                                                    }%> value="7" >7</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("8")) {
                                                        out.print("selected='selected'");
                                                    }%> value="8" >8</option>
                <option <%if (session.getAttribute("soluong" + temp_giohang).toString().equals("9")) {
                                                        out.print("selected='selected'");
                                                    }%> value="9" >9</option>
            </select>

        </td>
        <td align="center"><% out.print(df.format(Integer.parseInt(session.getAttribute("price" + temp_giohang).toString())));%> VND</td>
        <td align="center"><% out.print(df.format(Integer.parseInt(session.getAttribute("soluong" + temp_giohang).toString()) * Integer.parseInt(session.getAttribute("price" + temp_giohang).toString())));%> VND</td>
        <td align="center"><img onclick="XoaSP(<% out.print(session.getAttribute("idSP" + temp_giohang).toString());%>)" src="images/remove_x.gif" alt="remove" /><br />Remove </td>
            <%
                    if (total <= 2500000) {
                        total = total + Integer.parseInt(session.getAttribute("soluong" + temp_giohang).toString()) * Integer.parseInt(session.getAttribute("price" + temp_giohang).toString());
                        session.setAttribute("checktotal", "ok");
                    }
                    if (total > 2500000) {
                        session.setAttribute("checktotal", "fail");
                        flag_total = false;
                    }
                }
            %>
    </tr>
    <%
        }
    %>
    <tr style="height: 60px">

        <td colspan="3"  align="center" style="background:#ccc; font-weight:bold"> Total </td>
        <td colspan="2"  align="center" <%if (flag_total) {
                                    out.print("style='background:#ccc; font-weight:bold'");
                                } else {
                                    out.print("style='color:red; font-weight:bold'");
                                }%> ><% if (flag_total) {
                                        out.print(df.format(total) + " VND");
                                    } else {
                                        out.print("Sorry total your bill more than 2,500,000VND. Please delete some product");
                                    }%></td>

    </tr>
</table><% if (flag_total) {%>
<div style="float:right; width: 215px; margin-top: 20px;">

    <div class="checkout"><a href="<% if (session.getAttribute("success") != null && session.getAttribute("success").equals("success")) {
                                 out.print("Finshcheckout");
                             }%>" >Proceed to Checkout</a></div>
    <div class="cleaner h20"></div>
</div>
<% }%>

<%
            }
%>
<div ><a href="index.jsp">Continue Shopping</a></div>


